package company.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import company.common.JwtUtils;
import company.common.R;
import company.domain.Login;
import company.domain.Users;
import company.service.LoginService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;

@RestController
public class LoginController {
    @Resource
    LoginService loginService;

    /*
     * 用户登录
     * */
    @PostMapping("/Login")
    public R Login(@RequestBody Login login){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.setEntity(login);
        Login one = this.loginService.getOne(queryWrapper);
        if (one == null){
            return new R().setCode(502).setMessage("用户名密码错误");
        }else{
            HashMap<String,Object> hashMap=new HashMap<>();
            hashMap.put("id",one.getId());
            hashMap.put("name",one.getName());
            hashMap.put("pwd",one.getPwd());
            String jwtToken= JwtUtils.createJwtToken(hashMap);
            return new R().add("jwtToken",jwtToken);
        }
    }
}
